import { useAuthStore } from "@/stores/auth";
import axios from "axios";


class Http {

    constructor() {
        // 写一个Http类是为了构造围绕axios的一系列方法实现调用简化
        this.instance = axios.create({
            baseURL: import.meta.env.VITE_BASE_URL,
            timeout: 6000
        });

        this.instance.interceptors.request.use((config) => {
            const authStore = useAuthStore()
            const token = authStore.token
            if(token){
                config.headers.Authorization = "jwt " + token
            }
            return config
        })

        // this.instance.interceptors.response.use
    }

    post(path, data) {
        // path: /auth/login
        // url: http://127.0.0.1:8000/auth/login
        // return this.instance.post(path, data)
        // 上面直接返回太耗时，改用异步请求实现
        // 实际上axios也是使用Promise请求的，这里相当于简化了resolve, reject,拦截了无关的数据
        return new Promise(async (resolve, reject) => {
            try{
                let result = await this.instance.post(path, data)
                resolve(result.data)
            }catch(err){
                // 当状态吗不是200时
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    get(path, params) {
        return new Promise(async (resolve, reject) => {
            try{
                let result = await this.instance.get(path, {params})
                // params就是拼接查询字符串到url，其他常用的config还有headers：{}，比如可能某个get请求get(path, config={params：{}， headers：{'X-Custom-Header': 'custom value'}})
                resolve(result.data)
            }catch(err){
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    put(path, data) {
        return new Promise(async (resolve, reject) => {
            try{
                let result = await this.instance.put(path, data)
                resolve(result.data)
            }catch(err){
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    
    delete(path) {
        return new Promise(async (resolve, reject) => {
            try{
                let result = await this.instance.delete(path)
                // data是空的，后端仅返回状态码
                resolve(result)
            }catch(err){
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    downloadFile(path, params) {
        return new Promise(async (resolve, reject) => {
            try{
                let result = await this.instance.get(path, {
                    params,
                    // 二进制文件
                    responseType: "blob"
                })
                // params就是拼接查询字符串到url，其他常用的config还有headers：{}，比如可能某个get请求get(path, config={params：{}， headers：{'X-Custom-Header': 'custom value'}})
                resolve(result)
            }catch(err){
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }
}
// 直接导出实例，避免重复创建
export default new Http